////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   CustomIterateTag.java
// AUTHOR           :   ric.yik
// DATE CREATE      :   2006/6/22
// DESCRIPTION      :   
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/06/22   Ric Yik         1.0                 BugZilla #5535
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.framework.controller.taglib;

import org.apache.struts.taglib.logic.IterateTag;

import com.erp.framework.controller.form.MultiRow;
import com.erp.framework.util.logger.Logger;

public class CustomIterateTag extends IterateTag {

    private boolean useExtensions;

    private boolean useMultiRowIdx;

    public boolean getUseExtensions() {
        return useExtensions;
    }

    public void setUseExtensions(boolean useExtensions) {
        this.useExtensions = useExtensions;
    }

    public boolean getUseMultiRowIdx() {
        return useMultiRowIdx;
    }

    public void setUseMultiRowIdx(boolean useMultiRowIdx) {
        this.useMultiRowIdx = useMultiRowIdx;
    }

    public int getIndex() {
        if (!this.getUseExtensions() || !this.getUseMultiRowIdx()) return super.getIndex();
        if (started) {
            Object element = pageContext.findAttribute(id);
            if (element == null || (!(element instanceof MultiRow))) return super.getIndex();
            try {
                return Integer.parseInt(((MultiRow) element).getIdx());
            } catch (Exception e) {
                Logger.debug(this, "Cannot get idx from the MultiRow");
                return super.getIndex();
            }
        } else {
            return super.getIndex();
        }
    }

    public void release() {
        super.release();
        useExtensions = false;
        useMultiRowIdx = false;
    }

}
